#include "StdAfx.h"
#include "TinyDevicePool.h"
#include "TinyServerDefine.h"

namespace HAL{
	CTinyDevicePool::CTinyDevicePool(void)
	{
	}

	CTinyDevicePool::~CTinyDevicePool(void)
	{
	}

	CAnalogPhone* CTinyDevicePool::GetExtensionByID(string deviceID)
	{
		return this->m_aPhones[deviceID];
	}

	CAnalogTrunk* CTinyDevicePool::GetTrunkByID(string deviceID)
	{
		return this->m_aTrunks[deviceID];
	}
	
	void CTinyDevicePool::init()
	{
		//init extension and trunk deive
		std::vector<CTinyDevice*>* aphones = g_TinyCCDBMgr.extensions->GetAll();
		std::vector<CTinyDevice*>* atrunks = g_TinyCCDBMgr.trunks->GetAll();
		//std::vector<CVoiceCardEntry*>* acards = g_TinyCCDBMgr.cards->GetAll();
		//g_TinyCCDBMgr.trunks.loadData();

		if(aphones == NULL|| atrunks == NULL)
		{
			g_Logger.log(error,"init Device error from DB!");
			return;
		}

		//fill device vector
		for(int i =0;i<aphones->size();++i)
			m_aPhones.insert(std::pair<string,CAnalogPhone*>(((CAnalogPhone*)(aphones->at(i)))->extNumber,(CAnalogPhone*)(aphones->at(i))));

		for(int i =0;i<atrunks->size();++i)
			m_aTrunks.insert(std::pair<string,CAnalogTrunk*>(((CAnalogTrunk*)(atrunks->at(i)))->extNumber,(CAnalogTrunk*)(atrunks->at(i))));

		delete aphones;
		delete atrunks;
	}

	void CTinyDevicePool::ProcessReceivedMsg(CTinyMsg& inMsg,CTinyMsg& OutMsg)
	{
		switch(inMsg.getMsgIndex())
		{
		case VOICECARD_ACTION:
			switch(inMsg.getMsgItem())
			{
			case VOICECARD_ALERT:
			case VOICECARD_CONNECTED:
				g_TalkObjectPool.ProcessReceivedMsg(inMsg,OutMsg);
				break;
			case VOICECARD_REGISTDEVICE:
				{
					std::string moduleName,deviceType,cardtype,cardsn,channelId;
					inMsg.getDefaultParamByIndex(VOICECARD_REGISTDEVICE_MODULENAME,moduleName);
					inMsg.getDefaultParamByIndex(VOICECARD_REGISTDEVICE_DEVICETYPE,deviceType);
					inMsg.getDefaultParamByIndex(VOICECARD_REGISTDEVICE_CARDTYPE,cardtype);
					inMsg.getDefaultParamByIndex(VOICECARD_REGISTDEVICE_CARDSN,cardsn);
					inMsg.getDefaultParamByIndex(VOICECARD_REGISTDEVICE_CHANNELID,channelId);
					std::string deviceid = GetDeviceID(cardtype,cardsn,channelId,deviceType);
					OutMsg.setMsgIndex(VOICECARD_ACTION);
					if(deviceid != "")
					{
						OutMsg.setMsgItem(VOICECARD_REGISTDEVICE_SUCC);
						OutMsg.setDefaultParam(VOICECARD_REGISTDEVICE_SUCC_MODULENAME,moduleName);
						OutMsg.setDefaultParam(VOICECARD_REGISTDEVICE_SUCC_CARDTYPE,cardtype);
						OutMsg.setDefaultParam(VOICECARD_REGISTDEVICE_SUCC_CARDSN,cardsn);
						OutMsg.setDefaultParam(VOICECARD_REGISTDEVICE_SUCC_CHANNELID,channelId);
						OutMsg.setDefaultParam(VOICECARD_REGISTDEVICE_SUCC_DEVICETYPE,deviceType);
						OutMsg.setDefaultParam(VOICECARD_REGISTDEVICE_SUCC_REGDEVICEID,deviceid);
					}
					else
					{
						OutMsg.setDefaultParam(VOICECARD_REGISTDEVICE_ERROR_MODULENAME,moduleName);
						OutMsg.setMsgItem(VOICECARD_REGISTDEVICE_ERROR);
					}


					//OutMsg.setDefaultParam();
				}
				break;
			default:
				break;
			}
			break;
		default:
			break;
		};

	}

	std::string CTinyDevicePool::GetDeviceID(std::string CardType,std::string CardSN,std::string ChannelID,std::string DeviceType)
	{
		if(DeviceType == "Extension")
		{
			std::map<std::string,CAnalogPhone*>::iterator it = m_aPhones.begin();
			for(; it!= m_aPhones.end();++it)
			{
				if(it->second->m_CardType == CardType&&it->second->m_CardSN == CardSN&&it->second->m_ChannelID == atoi(ChannelID.c_str()))
					return it->second->extNumber;
			}

		}
		if(DeviceType == "Trunk")
		{
			std::map<std::string,CAnalogTrunk*>::iterator it = m_aTrunks.begin();
			for(; it!= m_aTrunks.end();++it)
			{
				if(it->second->m_CardType == CardType&&it->second->m_CardSN == CardSN&&it->second->m_ChannelID == atoi(ChannelID.c_str()))
					return it->second->extNumber;
			}

		}
		return std::string();

	}



}